<!-- 需求列表  -->
<template>
  <pageHeader
    title="产品BOM历史版本"
    :breadCrumb="['工厂建模', '产品BOM管理', '历史版本']"
    show-back
  />
  <div class="table-info">
    <div class="search-box">
      <a-space>
        <a-select
          v-model="searchForm.state"
          placeholder="状态"
          :style="{ width: '220px' }"
          @change="Search"
        >
          <a-option :value="''"> 全部 </a-option>
          <a-option :value="1"> 启用 </a-option>
          <a-option :value="2"> 停用 </a-option>
        </a-select>
      </a-space>
      <a-space>
        <a-button type="outline" @click="resetting">
          <template #icon> <icon-refresh /> </template>重置
        </a-button>
        <HeaderSettingCopy
          v-model:headerValues="headerValues"
          :define-headers="defineHeaders"
          :type="101"
        />
      </a-space>
    </div>
    <tableTurn :tableData="getDemandAll" :args="args" ref="tableRef">
      <a-table-column
        v-for="header in headerValues"
        :key="header.name"
        :width="header.width || 200"
        :title="header.name"
        :fixed="header.fixed"
        :data-index="header.code"
        align="center"
      >
        <template #cell="{ record }" v-if="!header.code">
          <template v-if="header.name === '状态'">
            <a-typography-text type="success" v-if="record.state === 1">
              启用
            </a-typography-text>
            <a-typography-text type="danger" v-if="record.state === 2">
              停用
            </a-typography-text>
          </template>
        </template>
      </a-table-column>
      <a-table-column :width="210" fixed="right" title="操作" align="center">
        <template #cell="{ record }">
          <a-space>
            <a-switch
              @click="switchClick(record)"
              :model-value="record.historyState"
              :checked-value="1"
              :unchecked-value="2"
            />
          </a-space>
        </template>
      </a-table-column>
    </tableTurn>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, onMounted, ref, nextTick } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  import {
    GetProductBomPage,
    DelProductBom,
    CheckProductBom,
    EditProductBomHistoryState,
  } from '@/api/FactoryModeling';
  import { Message } from '@arco-design/web-vue';

  const router = useRouter();
  const route = useRoute();

  let searchForm = reactive<any>({});
  // 表格配置
  const args = reactive({
    stripe: false,
    rowSelection: {
      type: 'checkbox',
      showCheckedAll: true,
      onlyCurrent: false,
    },
    bordered: {
      headerCell: true,
    },
    rowKey: 'id', // 批量选择按钮 以合同名称来作为key
  });
  const tableRef = ref();

  // 获取需求列表
  const getDemandAll = async (page: any) => {
    try {
      let res = await GetProductBomPage({
        ...page,
        ...searchForm,
        historyState: 1,
        id: route.query.saveId,
        bomCode: route.query.bomCode,
      });
      return Promise.resolve(res);
    } catch (err: unknown) {
      return Promise.resolve();
    }
  };

  const Search = () => {
    tableRef.value.loadData();
  };

  const resetting = () => {
    for (const key in searchForm) {
      delete searchForm[key];
    }
    tableRef.value.loadData();
  };
  let switchClick = async ({ historyState, id, bomCode }: any) => {
    if (historyState === 1) return Message.error('请通过启用其他历史版本关闭');
    await EditProductBomHistoryState({ id, bomCode });
    Search();
  };
  const del = async (id: number, bomCode: string) => {
    let check = await CheckProductBom({ treeId: id });
    if (!check.length) {
      await DelProductBom({ id, bomCode });
      tableRef.value.loadData();
    } else {
      Message.error('该BOM已被其他BOM引用，不可删除');
    }
  };
  let headerValues = ref<any[]>([]);
  let defineHeaders = [
    { code: 'bomCode', name: 'BOM编码', width: 200 },
    { code: 'bomName', name: 'BOM名称', width: 200 },
    { name: '产品编码' },
    { name: '产品名称' },
    { name: '规格型号' },
    { name: '基本单位' },
    { name: '状态' },
  ];
</script>

<style scoped lang="less">
  .table-info {
    padding: 10px;

    .search-box {
      margin-bottom: 16px;
      display: flex;
      justify-content: space-between;
    }
  }

  .use-state {
    display: flex;
    justify-content: center;
    text-align: center;
    .cycle {
      position: relative;
      width: 16px;
      height: 16px;
      border-radius: 50%;
      content: '';
      display: inline-block;
      top: 3px;
      right: 10px;
    }
  }

  .greyColor {
    color: #c9cdd4;
  }
  .greyColor-cycle {
    background-color: #c9cdd4;
  }

  // 审核中、执行中
  .blueColor {
    color: #156dff;
  }
  .blueColor-cycle {
    background-color: #156dff;
  }

  // 已通过
  .greenColor {
    color: #31c273;
  }
  .greenColor-cycle {
    background-color: #31c273;
  }

  // 不通过
  .redColor {
    color: #f53f3f;
  }
  .redColor-cycle {
    background-color: #f53f3f;
  }
</style>
